#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import asyncio
import sys
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root / 'src'))

from database.mongodb_client import MongoDBClient
from config import load_config

async def check_conversations():
    """检查MongoDB中的对话数据结构"""
    print("=== 检查MongoDB对话数据结构 ===")
    
    # 加载配置
    config = load_config()
    
    # 连接MongoDB
    client = MongoDBClient(config)
    await client.connect()
    
    try:
        # 获取一个示例对话
        sample = await client.database.conversations.find_one()
        if sample:
            print("\n示例对话数据结构:")
            for key, value in sample.items():
                if isinstance(value, str) and len(value) > 100:
                    print(f"  {key}: {value[:100]}...")
                else:
                    print(f"  {key}: {value}")
        else:
            print("\n❌ 没有找到对话数据")
        
        # 统计对话数量
        count = await client.database.conversations.count_documents({})
        print(f"\n对话总数: {count}")
        
        # 检查conversation_id字段
        conversations_with_id = await client.database.conversations.count_documents({"conversation_id": {"$exists": True}})
        print(f"包含conversation_id字段的对话数: {conversations_with_id}")
        
        # 获取几个conversation_id示例
        cursor = client.database.conversations.find({"conversation_id": {"$exists": True}}, {"conversation_id": 1, "_id": 1}).limit(5)
        print("\nconversation_id示例:")
        async for doc in cursor:
            print(f"  _id: {doc['_id']}, conversation_id: {doc.get('conversation_id', 'N/A')}")
            
    finally:
        await client.close()

if __name__ == "__main__":
    asyncio.run(check_conversations())